package com.duobaoyu.dby.supply.biz.processor;

import com.duobaoyu.dby.supply.biz.function.TransactionAfterFunction;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * 事务后置处理
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/26 11:20
 */
public class TransactionAfterPostProcessor {

	/**
	 * 如果存在事务则事务后执行，如果不存在事务则直接执行
	 *
	 * @param transactionAfterFunction 自定义函数
	 */
	public void execute(TransactionAfterFunction transactionAfterFunction) {
		if (TransactionSynchronizationManager.isSynchronizationActive()) {
			TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
				@Override
				public void afterCommit() {
					transactionAfterFunction.execute();
				}
			});
		} else {
			transactionAfterFunction.execute();
		}

	}
}
